package com.seagull.sb.dao.impl;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.seagull.sb.dao.NoticeDAO;
import com.seagull.sb.domain.Notice;
import com.seagull.sb.domain.NoticeSubject;
import com.seagull.sb.dto.NoticeDto;
import com.seagull.sb.util.SBRequestContext;
import com.seagull.sb.util.SBUtil;

public class NoticeDAOImpl extends BaseDAO implements NoticeDAO {

	/*
	 * (non-Javadoc)
	 * @see com.seagull.sb.dao.NoticeDAO#getNotice(long)
	 * 
	 * 
		CREATE TABLE `event` (
		  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
		  `class_id` bigint(20) unsigned NOT NULL,
		  `org_id` bigint(20) unsigned NOT NULL,
		  `name` varchar(200) NOT NULL,
		  `type` char(1) NOT NULL COMMENT 'E-Event, N-Notification',
		  `details` varchar(1000) NOT NULL,
		  `date` date NOT NULL,
		  `notification_type` varchar(100) NOT NULL,
		  `created_by` bigint(20) NOT NULL,
		  `created_dt` datetime NOT NULL,
		  `modified_dt` datetime DEFAULT NULL,
		  PRIMARY KEY (`id`)
		) ENGINE=InnoDB AUTO_INCREMENT=32 DEFAULT CHARSET=latin1;
	 */
	@Override
	public List<Notice> getNotice(SBRequestContext rc) throws Exception {
		Map<String,Object> qmap = new HashMap<String, Object>();
		
		qmap.put("org_id", new Long(rc.getOrgId()));
		if(rc.getClassId()>0){
			qmap.put("class_id", new Long(rc.getClassId()));
		}
		Object result = sqlMap.queryForList("noticeDB.getNotice",qmap);
		if(result instanceof List){
			return (List<Notice>)result;
		}
		return Collections.EMPTY_LIST;
	}

	@Override
	public List<Notice> getNotice(SBRequestContext rc, NoticeDto noticeDto) throws Exception {
		Map<String,Object> qmap = new HashMap<String, Object>();
		
		qmap.put("org_id", new Long(rc.getOrgId()));
		if(rc.getClassId()>0){
			qmap.put("class_id", new Long(rc.getClassId()));
		}
		if(noticeDto.getId() > 0){
			qmap.put("id", new Long(noticeDto.getId()));
		}
		Object result = sqlMap.queryForList("noticeDB.getNotice",qmap);
		if(result instanceof List){
			return (List<Notice>)result;
		}
		return Collections.EMPTY_LIST;
	}
	
	@Override
	public boolean saveNotice(SBRequestContext rc, Notice notice) throws Exception {
		boolean result = false;
		
		Map<String,Object> qmap = new HashMap<String, Object>();
		
		qmap.put("org_id", new Long(notice.getOrgId()));
		qmap.put("class_id", new Long(notice.getClassId()));
		qmap.put("name", notice.getTitle());
		qmap.put("type", notice.getType());
		qmap.put("details", notice.getDetails());
		qmap.put("notification_type", notice.getNoteType());
		qmap.put("created_by", notice.getCreatedBy());
		qmap.put("created_dt", SBUtil.getSystemDate());
		qmap.put("modified_dt", SBUtil.getSystemDate());
		
		Integer id = (Integer) sqlMap.insert("noticeDB.saveNotice",qmap);
		notice.setId(id.intValue());
		
		System.out.println("Notice Id Save : "+id.toString());
		result = true;		
		
		return result;
	}

	@Override
	public boolean saveNoticeAudit(SBRequestContext rc, Notice notice) throws Exception {
		boolean result = false;
		
		Map<String,Object> qmap = new HashMap<String, Object>();

		qmap.put("org_id", new Long(notice.getOrgId()));
		qmap.put("class_id", new Long(notice.getClassId()));
		qmap.put("notice_id", new Long(notice.getId()));
		
		sqlMap.insert("noticeDB.saveNoticeAudit",qmap);
		result = true;		
		
		return result;
	}

	@Override
	public List<NoticeSubject> getNoticeSubjects(SBRequestContext rc) throws Exception {
		Map<String,Object> qmap = new HashMap<String, Object>();
		
		qmap.put("org_id", new Long(rc.getOrgId()));
		/*if(rc.getClassId()>0){
			qmap.put("class_id", new Long(rc.getClassId()));
		}*/
		Object result = sqlMap.queryForList("noticeDB.getNoticeSubjects",qmap);
		if(result instanceof List){
			return (List<NoticeSubject>)result;
		}
		return Collections.EMPTY_LIST;
	}
}
